//
//  AppDelegate.m
//  AppManagement
//
//  Created by 珍 on 16/5/12.
//  Copyright © 2016年 abb.is.dev.eas. All rights reserved.
//

#import "AppDelegate.h"

#import "LeftViewController.h"
#import "AppPlistController.h"
#import "ExSlideMenuController.h"
#import "MessageViewController.h"

@interface AppDelegate ()
@property (nonatomic, strong) AppPlistController *appPlistVC;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
//    [self setAPNS];
//    [[UIApplication sharedApplication] cancelAllLocalNotifications];
//    [self setBadgeNumber];
    [self setupUI];
  


    return YES;
}



- (void)setupUI{
    self.window = [[UIWindow alloc]init];
    self.window.frame = [UIScreen mainScreen].bounds;
    
    AppPlistController *appVC = [[ AppPlistController alloc]init];
    LeftViewController *LeftVC = [[LeftViewController alloc]initWithNibName:@"LeftViewController" bundle:nil];
    UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:appVC];
    
    LeftVC.appPlistViewControler = nvc;
    [UINavigationBar appearance].tintColor = [UIColor yellowColor];
    
    ExSlideMenuController *slideMenuController = [[ExSlideMenuController alloc] initWithMainViewController:nvc leftMenuViewController:LeftVC rightMenuViewController:nil];
    slideMenuController.automaticallyAdjustsScrollViewInsets = YES;
    
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = slideMenuController;
    [self.window makeKeyAndVisible];
    



}

#pragma mark -APNs
- (void) applicationDidEnterBackground:(UIApplication *)application {
    
    [self setBadgeNumber];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    // 处理推送消息
    NSLog(@"收到推送消息:%@",userInfo);
    NSMutableDictionary *dict = [userInfo mutableCopy];
    [dict removeObjectForKey:@"aps"];
    
//    BOOL result = [[JFDataBase shareDataBaseWithDBName:kChatDatabaseName] queryTable:kMessageTableName ifExistsWithConditions:@{@"MESSAGE_ID": [userInfo objectForKey:@"MESSAGE_ID"]} withNewObject:dict];
//    if (result) {
//        
//        [self setBadgeNumber];
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"reciveNotification" object:nil];
//    }
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // re-post ( broadcast )
    NSString* token = [[[[deviceToken description]
                         stringByReplacingOccurrencesOfString: @"<" withString: @""]
                        stringByReplacingOccurrencesOfString: @">" withString: @""]
                       stringByReplacingOccurrencesOfString: @" " withString: @""];
    
    
//    NSString *requestURL = [NSString stringWithFormat:@"%@/app/agentClient/addDeviceToken?deviceToken=%@",kHostSite,token];
//    [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"deviceToken"];
//    [[HttpRequest alloc] postWithURLString:requestURL parameters:nil WithSuccess:^(id responseObject) {
//        
//        if (responseObject != nil) {
//            
//            id result = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
//            if (result == nil) {
//                
//                NSLog(@"推送请求返回值出错");
//                return;
//            }
//            if ([[[result objectForKey:@"status"] objectForKey:@"code"] integerValue] == 0) {
//                
//                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"canPush"];
//            }
//            else {
//                
//                NSLog(@"请求出错 errorCode:%@,message:%@",[[result objectForKey:@"status"] objectForKey:@"code"],[[result objectForKey:@"status"] objectForKey:@"msg"]);
//            }
//        }
//    } failure:^(id errorObject) {
//        
//        NSLog(@"%@",errorObject);
//    }];
}
- (void) setBadgeNumber {
    
//    NSArray *res = [[JFDataBase shareDataBaseWithDBName:kChatDatabaseName] queryTableWithSql:@"select * from message_info where STATE == 0"];
//    int badgeNumber = [res count];
//    [UIApplication sharedApplication].applicationIconBadgeNumber = badgeNumber;
}
- (void) setAPNS {
    // 开启APNS推送
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
    else {
        
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeAlert |
          UIRemoteNotificationTypeBadge |
          UIRemoteNotificationTypeSound)];
    }
}






- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
